//
//  TLMsgNotificationViewController.m
//  WXReader
//
//  Created by huaya on 2025/3/20.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMsgNotificationViewController.h"

#import "TLMsgNotTableViewCell.h"
#import "TLMsgModel.h"
#import "TLNotiThumbedModel.h"
#import "TL_CommentsViewController.h"
#import "TL_CommentReplyListViewController.h"

@interface TLMsgNotificationViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSNumber *pageNum;
@property (nonatomic, strong) NSNumber *pageSize;
@property (nonatomic, strong) NSMutableArray<TLNotiThumbedModel *> *dataArr;
@property (nonatomic, strong) UIView *tapView;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) UILabel *leftRedLab;
@property (nonatomic, strong) UILabel *rightRedLab;
@end

@implementation TLMsgNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray new];
    self.pageNum = [NSNumber numberWithInt:1];
    self.pageSize = [NSNumber numberWithInt:20];
    
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#F8F8FA"]];
    
    UIView *centerView = [[UIView alloc] initWithFrame:CGRectMake(34, 15, kScreenWidth-34*2, 40)];
    [centerView setBackgroundColor:[UIColor colorWithHexString:@"#EEEEEF"]];
    centerView.layer.cornerRadius = 20;
    
    [self.view addSubview:centerView];
    
    UIView *tapView = [[UIView alloc] init];
    tapView.frame = CGRectMake(4,4,(kScreenWidth-34*2-8)/2,32);
    tapView.backgroundColor = [UIColor whiteColor];
    tapView.layer.cornerRadius = 16;
    tapView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.0800].CGColor;
    tapView.layer.shadowOffset = CGSizeMake(0,2);
    tapView.layer.shadowOpacity = 1;
    tapView.layer.shadowRadius = 16;
    [centerView addSubview:tapView];
    self.tapView = tapView;
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, centerView.frame.size.width/2, 40)];
    [leftBtn setTitle:@"Replies received" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor hex_666666] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor hex_9E6DFC] forState:UIControlStateSelected];
    leftBtn.titleLabel.font = BOLD_SYSTEM_FONT(14);
    [leftBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.tag = 200;
    [centerView addSubview:leftBtn];
    self.leftBtn = leftBtn;
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(centerView.frame.size.width/2, 0, centerView.frame.size.width/2, 40)];
    [rightBtn setTitle:@"Likes History" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor hex_666666] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor hex_9E6DFC] forState:UIControlStateSelected];
    rightBtn.titleLabel.font = BOLD_SYSTEM_FONT(14);
    rightBtn.tag = 100;
    [rightBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [centerView addSubview:rightBtn];
    self.rightBtn = rightBtn;
    
    leftBtn.selected = YES;
    
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
    [self.tableView.mj_header beginRefreshing];
    
    [centerView addSubview:self.leftRedLab];
    [centerView addSubview:self.rightRedLab];
}
- (void)switchBtnAction:(UIButton *)sender{
    
    self.leftBtn.selected = self.leftBtn == sender;
    self.rightBtn.selected = self.rightBtn == sender;
    
    [UIView animateWithDuration:0.5 animations:^{
        self.tapView.center = sender.center;
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F8F8FA"];
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLMsgNotTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLMsgNotTableViewCell"];
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;

        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
    }
    return _tableView;
}
- (void)resetFrame:(CGRect)frame{
    NSLog(@"%@",NSStringFromCGRect(frame));
    self.view.frame = frame;
}
- (void)loadLastData{
    
    self.pageNum = [NSNumber numberWithInt:1];
    
    [self.dataArr removeAllObjects];
    
    [self network];
}
- (void)loadMoreData{
    
    self.pageNum = [NSNumber numberWithInt:(self.pageNum.intValue+1)];
    
    [self network];
}
- (void)network{
    
    NSString *url = self.leftBtn.selected? [APIURLManager systemNoticeComments]:[APIURLManager systemNoticeLikes];
    
    NSDictionary *param = @{
        @"pageNum":self.pageNum,
        @"pageSize":self.pageSize,
    };
    
    [APIManager sendGetRequest:url parameters:param model:TL_UserInfoModel.class success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        if (success) {
            NSLog(@"%@",requestModel);
            
            NSArray *data = [TLNotiThumbedModel mj_objectArrayWithKeyValuesArray:requestModel.data[@"records"]];
            
            [self.dataArr addObjectsFromArray:data];
            
            [self.tableView.mj_header endRefreshing];
            
            if (data.count<self.pageSize.intValue) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
    
    
}

// 添加自动布局调整
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 55, kScreenWidth, self.view.size.height-45);
}

#pragma mark - table delegate - datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TLMsgNotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLMsgNotTableViewCell"];

    [cell setStatusWithType:self.leftBtn.selected];
    
    cell.dataModel = self.dataArr[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.leftBtn.selected) {
        return 145;
    }else{
        return 105;
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.leftBtn.selected) {
        
        TLNotiThumbedModel *model = self.dataArr[indexPath.row];
        
//        if ([model.worksType isEqualToString:@"0"]) {
//            //短篇
//            TLShortStoryViewController *vc = [TLShortStoryViewController new];
//            vc.worksId = model.worksId.stringValue;
//            [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
//        }else{
//            //长篇
//            TL_BookMallDetailViewController *vc = [TL_BookMallDetailViewController new];
//            vc.book_id = [model.worksId intValue];
//            [[TL_ViewHelper getCurrentViewController].navigationController pushViewController:vc animated:YES];
//        }
        
//        TL_CommentsViewController *vc = [[TL_CommentsViewController alloc] init];
//        vc.production_id = self.dataArr[indexPath.row].worksId.intValue;
//        vc.commentsSuccessBlock = ^(TL_CommentsDetailModel *commentModel) {
//           
//        };
//        [self.navigationController pushViewController:vc animated:YES];
        
        TL_CommentReplyListViewController *commentReplyListViewController = [[TL_CommentReplyListViewController alloc] init];
        commentReplyListViewController.commentId = model.parentCommentId;
        NavigationController *nav = [[NavigationController alloc] initWithRootViewController:commentReplyListViewController];
        [[TL_ViewHelper getCurrentViewController].navigationController presentViewController:nav animated:YES completion:^{
                    
        }];
    }
    
}
- (UILabel *)leftRedLab{
    if (!_leftRedLab) {
        _leftRedLab = [[UILabel alloc] init];
        _leftRedLab.backgroundColor = [UIColor redColor];
        _leftRedLab.layer.masksToBounds = YES;
        _leftRedLab.layer.cornerRadius = 8/2;
        _leftRedLab.hidden = YES;
        _leftRedLab.frame = CGRectMake(CGRectGetMaxX(self.leftBtn.frame)-30, CGRectGetMinY(self.leftBtn.frame)+10, 8, 8);
    }
    return _leftRedLab;
}
- (UILabel *)rightRedLab{
    if (!_rightRedLab) {
        _rightRedLab = [[UILabel alloc] init];
        _rightRedLab.backgroundColor = [UIColor redColor];
        _rightRedLab.layer.masksToBounds = YES;
        _rightRedLab.layer.cornerRadius = 8/2;
        _rightRedLab.hidden = YES;
        
        _rightRedLab.frame = CGRectMake(CGRectGetMaxX(self.rightBtn.frame)-40, CGRectGetMinY(self.leftBtn.frame)+10, 8, 8);
    }
    return _rightRedLab;
}
- (void)setUnReadModel:(TLUnReadModel *)unReadModel{
    if (unReadModel) {
        _unReadModel = unReadModel;
        if (_unReadModel.replyMessages.intValue>0) {
            self.leftRedLab.hidden = NO;
        }else{
            self.leftRedLab.hidden = YES;
        }
        if (_unReadModel.likeMessages.intValue>0) {
            self.rightRedLab.hidden = NO;
        }else{
            self.rightRedLab.hidden = YES;
        }
    }
}
@end
